package com.jindun.paper.ui.bag;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;

public class MultiValueInputControl extends Composite {

	private List<Text> textList = new ArrayList<Text> ();
	
	public MultiValueInputControl(Composite parent) {
		super(parent, SWT.BORDER);
		//Composite container = new Composite(parent, SWT.BORDER);
		this.setLayout(new FillLayout(SWT.VERTICAL));
		
		
		for (int j = 0; j < 5; j++ ) {
			Composite row = new Composite (this, SWT.None);
			row.setLayout(new FillLayout(SWT.HORIZONTAL));
			for (int i = 0; i < 10; i++) {
				Text text = new Text(row, SWT.BORDER);
				text.addListener(SWT.Verify, new Listener() {
				      public void handleEvent(Event e) {
				        String string = e.text;
				        char[] chars = new char[string.length()];
				        string.getChars(0, chars.length, chars, 0);
				        //e.doit = NumberUtils.isDigits(string);
//				        for (int i = 0; i < chars.length; i++) {
//				          if (!('0' <= chars[i] && chars[i] <= '9' || chars[i] == '.')) {
//				            e.doit = false;
//				            return;
//				          }
//				        }
				      }
				    });
				
				text.addListener(SWT.Modify, new Listener() {	
					@Override
					public void handleEvent(Event event) {
						MultiValueInputControl.this.notifyListeners(SWT.Modify, event);
						
					}
				});
				
				this.textList.add(text);
			}
		}
		
	}
	
	
	
	public Text getTextCtr(int index) {
		return textList.get(index);
	}

	public List<Text> getTextList() {
		return textList;
	}

	public void setTextList(List<Text> textList) {
		this.textList = textList;
	}

	public List<Double> getValues () {
		List<Double> list = new ArrayList<Double> ();
		for (Text text :  this.textList) {
			if (StringUtils.isNotBlank(text.getText())) {
				list.add(Double.valueOf(text.getText()));
			}
		}
		return list;
	}



	@Override
	public void setEnabled(boolean enabled) {
		super.setEnabled(enabled);
		
		for (Text text : textList) {
			text.setEnabled(enabled);
		}
		
		layout(true, true);
	}
	
	
	
	
	

	
}
